from api.handler.ce.AbstractBatchCE import AbstractBatchCE
from api.model.BatchOutputPipe import BatchOutputPipe
from api.model.FdInputPipe import FdInputPipe
from munch import Munch
# from config.annotation import Value
import json

def get_sex(id):
    if len(id) == 18:
        return '女' if int(id[-2])%2 == 0 else '男'
    else:
        return None

def get_birth(id):
    if len(id) == 18:
        return id[6:14]
    else:
        return None

class HrBasicInformationHandler(AbstractBatchCE):

    def do_compute(self, output_stream: BatchOutputPipe, source_fds: FdInputPipe, params: dict):

        fd = source_fds.get_fd('hr_user_data')
        sql = "select UserId,UserName,UserActualName,UserIdentity,DepName from hr_user_data"
        results = fd.query(sql)

        for result in results:
            data = Munch()
            data.id = result.get('UserId')
            data.usercode = result.get('UserName')
            data.username = result.get('UserActualName')
            data.depte = result.get('DepName')
            userid = result.get('UserIdentity')
            data.userid = userid
            data.sex = get_sex(userid)
            data.birth = get_birth(userid)
            output_stream.output_for_upsert(data)
